package hongcaosp.app.android.user.settings.safety.bind;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import hongcaosp.app.android.R;
import hongcaosp.app.android.login.RegisterActivity;
import hongcaosp.app.android.login.UserToken;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.utils.StatusBarUtil;

public class BindPhoneActivity extends BaseActivity {

    private BindPhoneFragment bindPhoneFragment;
    private CheckNowPhoneFragment checkNowPhoneFragment;
    private BindNewPhoneFragment bindNewPhoneFragment;

    private String src;

    @Override
    protected void initData(Intent intent) {

        src = intent.getStringExtra("src");
//        if ("main".equals(src)) {
//            findViewById(R.id.back_btn).setVisibility(View.GONE);
//        }

        String mobile = "";
        if (UserToken.getDefault().getUser() != null) {
            mobile = UserToken.getDefault().getUser().getMobile();
        }
        if (TextUtils.isEmpty(mobile)) {
            bindPhoneFragment();
        } else {
            checkNowPhoneFragment();
        }
    }

    @Override
    protected void initView() {
        StatusBarUtil.setStatusBarDarkTheme(this,false);
        findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("main".equals(src)) {
                    UserToken.getDefault().clearToken();
                    Intent intent = new Intent(BindPhoneActivity.this,RegisterActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    finish();
                } else {
                    onBackPressed();
                }
            }
        });
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_bind_phone;
    }

    @Override
    public void onBackPressed() {
        if ("main".equals(src)) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
        } else {
            super.onBackPressed();
        }
    }

    public void bindPhoneFragment() {
        if (bindPhoneFragment == null) {
            bindPhoneFragment = new BindPhoneFragment();
        }
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fl_container,bindPhoneFragment)
                .commit();
    }

    public void checkNowPhoneFragment() {
        if (checkNowPhoneFragment == null) {
            checkNowPhoneFragment = new CheckNowPhoneFragment();
        }
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fl_container,checkNowPhoneFragment)
                .commit();
    }

    public void bindNewPhoneFragment() {
        if (bindNewPhoneFragment == null) {
            bindNewPhoneFragment = new BindNewPhoneFragment();
        }
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fl_container,bindNewPhoneFragment)
                .commit();
    }
}
